﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Pg.BioMedics.SDR.Diagnostics
{
    /// <summary>
    /// Provides base functionality for all performance 
    /// counter wrappers
    /// </summary>
    public abstract class BasePerformanceCounter
    {
        #region Fields

        private string category;
        private string name;
        private PerformanceCounterType type;

        #endregion

        #region Constructors

        /// <summary>
        /// Provides base initialization for the performance counter.
        /// </summary>
        /// <param name="category">Performance counter category</param>
        /// <param name="name">Performance counter name</param>
        /// <param name="type">Performance counter type</param>
        internal protected BasePerformanceCounter(string category, string name, PerformanceCounterType type)
        {
            this.category = category;
            this.name = name;
            this.type = type;                      
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets performance counter category name
        /// </summary>
        protected string Category
        {
            get
            {
                return this.category;
            }
        }

        /// <summary>
        /// Gets performance counter name
        /// </summary>
        protected string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>
        /// Gets performance counter type
        /// </summary>
        protected PerformanceCounterType Type
        {
            get
            {
                return this.type;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Sends performance counter value to the WMI
        /// </summary>
        /// <param name="value">Performance counter value</param>
        protected void Apply(long value)
        {
            try
            {
                using (PerformanceCounter performanceCounter = new PerformanceCounter(this.category, this.name, false))
                {
                    performanceCounter.RawValue = value;
                }
            }
            catch { }
        }

        #endregion
    }
}
